package com.fanhao.dllo.justforyou.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Movie;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.JokePictureCommentActivity;
import com.fanhao.dllo.justforyou.view.GifMovieView;
import com.fanhao.dllo.justforyou.entity.JokePictureList;
import com.fanhao.dllo.justforyou.volley.GetVolleyDatasUtil;
import com.fanhao.dllo.justforyou.volley.StreamRequest;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/24.
 */
public class JokePictureAdapter extends RecyclerView.Adapter<
        JokePictureAdapter.JokePictureViewHolder> {
        private List<JokePictureList> list;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();

//    private ImageLoader.ImageListener imageListener;
    private Context context;
    private int width;
    private int height;

    // 设置点击跳转
    private JokePictureCommentOnClickListener listener;

    public interface JokePictureCommentOnClickListener {
        void OnItemClickListener(JokePictureList list);
    }
//    VolleySingleton volleySingleton = VolleySingleton.getInstance();
//    private ImageLoader imageLoader ;

//    public JokePictureAdapter() {
//        imageLoader = volleySingleton.getImageLoader();
//    }

//    public void setOnClickListener(JokeCommentOnClickListener jl) {
//    listener = jl;
//}

    public JokePictureAdapter(Context c){

        this.context = c;
    }

    public void addData(List<JokePictureList> datas) {
        list = datas;
        notifyDataSetChanged();
    }

    public void loadData(List<JokePictureList> datas) {
        list.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 缓存组建内部类
     * **/
    public class JokePictureViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imgIv;
        private NetworkImageView contentIv;
        private TextView nameTv,timeTv,contentTv,likeTv,
                dislikeTv,shareTv,commentTv;
        private GifMovieView gifMv;
        private int position;

        public JokePictureViewHolder(View v) {
            super(v);
            imgIv = (ImageView) v.findViewById(R.id.imgIv);
            contentIv = (NetworkImageView) v.findViewById(R.id.contentIv);
            gifMv = (GifMovieView)v.findViewById(R.id.gifMv);

            nameTv = (TextView)v.findViewById(R.id.nameTv);
            timeTv = (TextView)v.findViewById(R.id.timeTv);
            contentTv = (TextView)v.findViewById(R.id.contentTv);
            likeTv = (TextView)v.findViewById(R.id.likeTv);
            dislikeTv = (TextView)v.findViewById(R.id.dislikeTv);
            shareTv = (TextView)v.findViewById(R.id.shareTv);
            commentTv = (TextView)v.findViewById(R.id.commentTv);

            commentTv.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.commentTv:
                    if (list != null && list.size() > 0) {
                        JokePictureList pictureList = list.get(position);
                        Intent intent = new Intent(context, JokePictureCommentActivity.class);
                        intent.putExtra("jokePicId", pictureList.getId());
                        context.startActivity(intent);
                    }
                    break;
            }

        }
    }


    @Override
    public int getItemCount() {
        return list!= null && list.size() > 0
                ? list.size() : 0;
    }


    @Override
    public void onBindViewHolder(JokePictureViewHolder holder, int position) {

        if (list != null && list.size() > 0) {
            JokePictureList jokePictureList = list.get(position);

                holder.nameTv.setText(jokePictureList.getName());
                holder.timeTv.setText(jokePictureList.getCreate_time());
                holder.contentTv.setText(jokePictureList.getText());
                holder.likeTv.setText(jokePictureList.getLove());
                holder.dislikeTv.setText(jokePictureList.getHate());
                holder.shareTv.setText(jokePictureList.getRepost());
                holder.commentTv.setText(jokePictureList.getComment());

            width = Integer.parseInt(jokePictureList.getWidth());
            height = Integer.parseInt(jokePictureList.getHeight());

            if (jokePictureList.getProfile_image() != null &&
                    jokePictureList.getProfile_image().length() > 0) {
                String headImgUrl = jokePictureList.getProfile_image();
                GetVolleyDatasUtil.getBitmapRequest(headImgUrl, holder.imgIv);
            }
            if (jokePictureList.getImage0() != null &&
                    jokePictureList.getImage0().length() > 0) {
//                String contentImgUrl = jokePictureList.getImage0();
//                getBitmap(contentImgUrl, holder.contentIv);
                String contentUrl = jokePictureList.getImage0();

                if (jokePictureList.getIs_gif().equals("1")) {
                    holder.contentIv.setVisibility(View.GONE);
                    holder.gifMv.setVisibility(View.VISIBLE);

                    holder.gifMv.getLayoutParams();
                    getGifMovie(contentUrl,holder.gifMv);
                } else if (jokePictureList.getIs_gif().equals("0")) {
                    holder.gifMv.setVisibility(View.GONE);
                    holder.contentIv.setVisibility(View.VISIBLE);
                    GetVolleyDatasUtil.getBitmapRequest(contentUrl, holder.contentIv);
                }

            }

           holder.position = position;
        }

    }

    private void getGifMovie(String url, final GifMovieView imageView) {
        StreamRequest request = new StreamRequest(url,
                new Response.Listener<InputStream>() {
                    @Override
                    public void onResponse(InputStream response) {
                        Movie movie = Movie.decodeStream(response);
                        imageView.setMovie(movie);
                        try {
                            response.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                           error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }



    @Override
    public JokePictureViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());
        View v = inflater.inflate(R.layout.fragment_joke_picture_item,null);
        return new JokePictureViewHolder(v);
    }
}
